import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:oi_ai_tool/config/firebase_options.dart';
import 'package:oi_ai_tool/route/oi_route.dart';
import 'package:oi_ai_tool/services/chat_service.dart';
import 'package:oi_ai_tool/services/user_service.dart';

import 'config/oi_theme_data.dart';

Future<void> main() async {
  initApp();
}

initApp() async {
  await googleFirebaseCrashlyticsInit();
  initServcies();
  runApp(const MyApp());
}

initServcies() {
  Get.put(UserService());
  Get.put(ChatService());
}

googleFirebaseCrashlyticsInit() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  FlutterError.onError = (errorDetails) {
    FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
  };
  // Pass all uncaught asynchronous errors that aren't handled by the Flutter framework to Crashlytics
  PlatformDispatcher.instance.onError = (error, stack) {
    FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
    return true;
  };
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(393, 852),
      minTextAdapt: true,
      splitScreenMode: true,
      useInheritedMediaQuery: true,
      builder: (_, child) {
        return GetMaterialApp(
          debugShowCheckedModeBanner: false,
          initialRoute: Routes.Initials,
          title: 'oi-tool',
          getPages: OiRoute.pages,
          defaultTransition: Transition.cupertino,
          theme: oiThemeData(),
          home: child,
          builder: EasyLoading.init(),
        );
      },
    );
  }
}
